package com.booklove.service.school;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.booklove.domain.dto.PageDTO;
import com.booklove.domain.po.Apply;
import com.booklove.domain.query.ApplyQuery;
import com.booklove.mapper.ApplyMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 专业 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-09-21
 */
@Service
public class ApplyServiceImpl extends ServiceImpl<ApplyMapper, Apply> implements ApplyService {

    @Override
    public PageDTO<Apply> queryApplysPage(ApplyQuery applyQuery) {
        String name = applyQuery.getName();
        // 1.添加排序规则
        // 使用内置的函数转换并且 设置规则
        List<OrderItem> orderItems = applyQuery.toMpOrderItems();

        // 获取分页对象和排序情况
        Page<Apply> page = applyQuery.toMpPageDefaultSortByIdAsc(orderItems.toArray(new OrderItem[0]));
        /*
        使用lambda的方式查询
         */
        Page<Apply> p = lambdaQuery()
                .like(name != null, Apply::getName, name)
                .page(page);

        // 获取数据信息

        return PageDTO.of(p, Apply.class);
    }

    @Override
    public void saveApply(Apply apply) {
        baseMapper.insert(apply);
    }

    @Override
    public void deleteById(Long id) {
        baseMapper.deleteById(id);
    }
}
